# coding:utf-8
from odoo import models, fields, api, _
from odoo.exceptions import Warning


class OLSaleAdvancePaymentInv(models.TransientModel):
    _inherit = "sale.advance.payment.inv"

    @api.multi
    def create_invoices(self):
        so = self.env['sale.order'].browse(self._context.get('active_ids', []))
        if self.advance_payment_method == 'fixed':
            if so.amount_untaxed - so.ol_deposit_amount - self.amount < 0:
                raise Warning(_('Deposit amount can not greater than order amount.'))
        if self.advance_payment_method == 'percentage':
            if so.amount_total - so.ol_deposit_amount - (so.amount_untaxed * self.amount / 100) < 0:
                raise Warning(_('Deposit amount can not greater than order amount.'))
        return super(OLSaleAdvancePaymentInv, self).create_invoices()